ubuntu nginx安裝及服務(wù)配置跨域問題處理方式
更新時(shí)間:2024年07月02日 10:40:20 作者:野生獼猴桃
這篇文章主要介紹了ubuntu nginx安裝及服務(wù)配置跨域問題處理方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
1、安裝nginx
apt-get install nginx
2、啟動(dòng)nginx服務(wù)
外部瀏覽器訪問默認(rèn)80端口
service nginx start
3、配置文件修改
vim /etc/nginx/nginx.conf
#user www-data; user root; #使用root worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; server { listen 8080; #監(jiān)聽端口 server_name 172.16.30.70; #當(dāng)前nginx部署機(jī)器地址,或是當(dāng)前機(jī)器的公網(wǎng)地址 # 全局跨域添加 #add_header 'Access-Control-Allow-Origin' '*'; #add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; #add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,PATCH,OPTIONS'; #add_header 'Access-Control-Allow-Headers' 'X-Requested-With'; location / { auth_basic off; #參數(shù)off表示不開啟HTTP基本認(rèn)證 root /root/webwork/dist; #前端項(xiàng)目路徑 index index.html index.htm; #index proxy_pass http://172.16.30.70:8085; #后端接口地址(要轉(zhuǎn)發(fā)的地址服務(wù)) # 跨域 add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,PATCH,OPTIONS'; add_header 'Access-Control-Allow-Headers' 'X-Requested-With'; ## proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; proxy_http_version 1.1; } } # 轉(zhuǎn)發(fā)10.7.57.29:6800到23.91.97.141:399 server { listen 39907; server_name 23.91.97.141; location / { auth_basic off; proxy_pass http://10.7.57.29:6800; } } # 負(fù)載均衡到10.7.187.21:18001 server { listen 18001; server_name 10.7.187.21; location / { proxy_pass http://destination-address1; } } upstream destination-address1 { server 10.7.173.36:18001; server 10.7.124.180:18001; } } #轉(zhuǎn)發(fā)redis端口到39906 stream { server { listen 39906; proxy_pass 10.7.187.21:6379; } } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
4、重置nginx且重啟
service nginx reload service nginx restart
5、卸載nginx
apt-get remove nginx nginx-common # 卸載刪除除了配置文件以外的所有文件。 apt-get purge nginx nginx-common # 卸載所有東東,包括刪除配置文件。 apt-get autoremove # 在上面命令結(jié)束后執(zhí)行,主要是卸載刪除Nginx的不再被使用的依賴包。 apt-get remove nginx-full nginx-common #卸載刪除兩個(gè)主要的包。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
簡介使用Nginx Plus的在線活動(dòng)監(jiān)控功能的方法
這篇文章主要介紹了簡介使用Nginx Plus的在線活動(dòng)監(jiān)控功能的方法,注意其目前暫時(shí)為收費(fèi)項(xiàng)目,需要的朋友可以參考下2015-06-06nginx配置請求轉(zhuǎn)發(fā)不生效的實(shí)現(xiàn)
本文主要介紹了nginx配置請求轉(zhuǎn)發(fā)不生效的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02Nginx出現(xiàn)403 Forbidden的四種解決方案分享
這篇文章主要給大家分享了關(guān)于Nginx出現(xiàn)403 Forbidden的四種解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09使用Nginx限制IP請求和并發(fā)連接數(shù)的實(shí)現(xiàn)方法
本文主要介紹了使用Nginx限制IP請求和并發(fā)連接數(shù)的實(shí)現(xiàn)方法,通過使用Nginx的限制模塊,我們可以輕松地實(shí)現(xiàn)對IP請求和并發(fā)連接數(shù)的限制,具體就跟小編一起來了解一下2024-03-03Windows環(huán)境實(shí)現(xiàn)Nginx配置及開機(jī)自啟動(dòng)
本文主要介紹了Windows環(huán)境實(shí)現(xiàn)Nginx配置及開機(jī)自啟動(dòng),通過兩種方式可以實(shí)現(xiàn)nginx的開機(jī)自啟動(dòng),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03