Nginx常用配置文件

nginx配置

均默认nginx.conf

创建新conf至conf.d目录

普通反代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server {
listen 80;
server_name 你的网站;
return 301 https://你的网站$request_uri;
}

server {
listen 443 ssl;
server_name 你的网站;
ssl_certificate /etc/ssl/你的网站.cer;
ssl_certificate_key /etc/ssl/你的网站.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
location / {
proxy_redirect off;
proxy_pass http://127.0.0.1:5679;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
#proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
}
}

直接修改nginx.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
server {
listen 80;
server_name 你的网站;
return 301 https://你的网站$request_uri;
}
server {
listen 443 ssl;
server_name 你的网站;
ssl_certificate /etc/ssl/fullchain.cer;
ssl_certificate_key /etc/ssl/你的网站.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# error_page 497 https://$host$request_uri;
location / {
proxy_pass http://127.0.0.1:25500; #对应 pref.ini 里的配置
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
}
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
}

反代静态网页:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
server
{
listen 80;
listen 443 ssl;
server_name 你的网站;
ssl on;
ssl_certificate /etc/ssl/你的网站.cer;
ssl_certificate_key /etc/ssl/你的网站.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
add_header Strict-Transport-Security "max-age=31536000";

if ( $scheme = http ){
return 301 https://$server_name$request_uri;
}

if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) {
return 403;
}

location / {
sub_filter 需要反代的网站 你的网站;
sub_filter_once off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer https://需要反代的网站;
proxy_set_header Host 需要反代的网站;
proxy_pass https://需要反代的网站;
proxy_set_header Accept-Encoding "";
}
}

反代本地html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
listen 80;
server_name 你的网站;
return 301 https://你的网站$request_uri;
}
server {
listen 443 ssl;
server_name 你的网站;
rewrite (.*)nem\/.nxnow\/.xyz(.*)$ $host$1 permanent;
ssl_certificate /etc/ssl/你的网站.cer;
ssl_certificate_key /etc/ssl/你的网站.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
#本地网页路径
location / {
root /home/dist;
try_files $uri $uri/ /index.html;
#index index.html;
}
}