ubuntu nginx安裝及服務(wù)配置跨域問題處理方式
更新時間:2024年07月02日 10:40:20 作者:野生獼猴桃
這篇文章主要介紹了ubuntu nginx安裝及服務(wù)配置跨域問題處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
1、安裝nginx
apt-get install nginx
2、啟動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 #卸載刪除兩個主要的包。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
NGINX 配置內(nèi)網(wǎng)訪問的實(shí)現(xiàn)步驟
本文主要介紹了NGINX 配置內(nèi)網(wǎng)訪問的實(shí)現(xiàn)步驟,Nginx的geo模塊限制域名訪問權(quán)限,僅允許內(nèi)網(wǎng)/辦公室IP訪問,具有一定的參考價值,感興趣的可以了解一下2025-05-05
Nginx中禁止使用IP訪問網(wǎng)站的配置實(shí)例
這篇文章主要介紹了Nginx中禁止使用IP訪問網(wǎng)站的配置實(shí)例,一般在備案時可能需要這種設(shè)置,需要的朋友可以參考下2014-07-07
nginx通過https部署vue項(xiàng)目的完整步驟
在實(shí)際開發(fā)中,我們會以https形式進(jìn)行頁面訪問,下面這篇文章主要給大家介紹了關(guān)于nginx通過https部署vue項(xiàng)目的完整步驟,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Nginx日志中request_time和upstream_response_time區(qū)別
Nginx日志中的request_time和upstream_response_time是關(guān)鍵的性能指標(biāo),本文就來介紹一下Nginx日志中request_time和upstream_response_time區(qū)別,具有一定的參考價值,感興趣的可以了解一下2024-11-11
Nginx反向代理出現(xiàn)502與504錯誤問題詳解及排查指南
Nginx 作為反向代理服務(wù)器時,502(Bad Gateway)和 504(Gateway Timeout)錯誤是常見的兩類問題,下面我們就來看看這兩個錯誤問題的排查與解決吧2025-05-05
nginx-rtmp-module構(gòu)建流媒體直播服務(wù)器實(shí)戰(zhàn)指南
本文主要介紹了nginx-rtmp-module構(gòu)建流媒體直播服務(wù)器實(shí)戰(zhàn)指南,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
Ubuntu中Nginx虛擬主機(jī)設(shè)置的項(xiàng)目實(shí)踐
通過配置虛擬主機(jī),可以在同一臺服務(wù)器上運(yùn)行多個獨(dú)立的網(wǎng)站,本文主要介紹了Ubuntu中Nginx虛擬主機(jī)設(shè)置的項(xiàng)目實(shí)踐,具有一定的參考價值,感興趣的可以了解一下2025-03-03

