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?生產(chǎn)環(huán)境安全配置加固的實(shí)現(xiàn)
本文主要介紹了Nginx?生產(chǎn)環(huán)境安全配置加固的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
Nginx 反向代理與負(fù)載均衡:一臺服務(wù)器扛不住的解決方案
文章主要介紹了如何使用Nginx解決單臺服務(wù)器負(fù)載過高的問題,通過Nginx的反向代理和負(fù)載均衡功能,可以將請求分散到多臺Tomcat服務(wù)器上,同時(shí)處理靜態(tài)資源和動(dòng)態(tài)請求,并配置Gzip壓縮、HTTPS、限流等以提高效率和安全性,文中還提供了詳細(xì)的配置步驟和示例代碼2026-04-04
Nginx中日志模塊的應(yīng)用和配置應(yīng)用示例
Nginx是一款高性能的HTTP和反向代理服務(wù)器,廣泛應(yīng)用于互聯(lián)網(wǎng)領(lǐng)域,這篇文章主要介紹了Nginx中日志模塊的應(yīng)用和配置,下面通過一個(gè)簡單的實(shí)例來演示Nginx日志模塊的應(yīng)用和配置,需要的朋友可以參考下2024-02-02
Nginx 合并請求連接且加速網(wǎng)站訪問實(shí)例詳解
這篇文章主要介紹了Nginx 合并請求連接且加速網(wǎng)站訪問實(shí)例詳解,瀏覽器的并發(fā)請求數(shù)目限制是針對同一域名的,同一時(shí)間針對同一域名下的請求有一定數(shù)量限制,超過限制數(shù)目的請求會被阻塞,需要的朋友可以參考下2019-07-07
關(guān)于使用Keepalived實(shí)現(xiàn)Nginx的自動(dòng)重啟及雙主熱備高可用問題
這篇文章主要介紹了使用Keepalived實(shí)現(xiàn)Nginx的自動(dòng)重啟及雙主熱備高可用,本文通過幾個(gè)問題解析幫助大家學(xué)習(xí)Keepalived實(shí)現(xiàn)Nginx的自動(dòng)重啟的相關(guān)知識,需要的朋友可以參考下2021-09-09

