参考:
安装 nginx
sudo yum install -y nginx启动 nginx
sudo systemctl start nginx开机自启动 nginx
sudo systemctl enable nginx补充:
关闭 nginx
sudo systemctl stop nginx重启 nginx
sudo systemctl restart nginx查看状态 nginx
sudo systemctl status nginx/usr/share/nginx/html/etc/nginx/conf.d/default.conf/etc/nginx/conf.d//etc/nginx/nginx.conf参考:
参考:
/etc/nginx/conf.d/default.conf
# HTTP 跳转到 HTTPS
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl; # 此处如未添加ssl,可能造成Nginx无法启动。
server_name example.com www.example.com; # 证书绑定的域名,例如:www.example.com。多个域名用空格分开。
ssl_certificate /etc/nginx/ssl/example/example.com.pem; # 证书的文件名
ssl_certificate_key /etc/nginx/ssl/example/example.com.key; # 证书的密钥文件名
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers HIGH:!aNULL:!MD5;
#access_log off; # 反向代理如果并发大,务必要关闭日志,否则IO吃紧。
# 方式1: 反向代理: 将请求转发到 ASP.NET Core 端口 5000
location / {
proxy_pass http://localhost:5000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 方式2: 静态站点
location / {
root /usr/share/nginx/html; # 站点目录。
index index.html index.htm; # 默认索引页
}
}
server_name、ssl_certificate、ssl_certificate_key、proxy_pass需替换为你自己的值。 只需要选择一个方式即可,而不是两个location /都写
补充:
HTTP 跳转到 HTTPS 方法2:
重写:
server {
listen 80;
server_name www.example.com example.com;
rewrite ^(.*)$ https://$host$1 permanent; # 将所有http请求通过rewrite重定向到https。
}user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
include mime.types;
#include luawaf.conf;
include proxy.conf;
default_type application/octet-stream;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server_tokens off;
access_log off;
include /www/server/panel/vhost/nginx/*.conf; # 引入 /www/server/panel/vhost/nginx/ 位置的全部配置文件
}/Upload 开头 url 允许跨域
location /Upload {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
}补充
server
{
listen 80;
listen 443 ssl http2;
server_name api-onetree.moeci.com;
root /www/wwwroot/api-onetree.moeci.com;
location /api {
proxy_pass http://localhost:5000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /install {
proxy_pass http://localhost:5000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /Upload {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
}
}注意: 发现 nginx 的
location对大小写敏感
参考:
编辑 nginx.conf 文件
在 http {} 中添加
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}在你的 server {} location / {} 中添加
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";例如下方
server
{
listen 80;
listen 443 ssl http2;
server_name plugincore.moeci.com;
#index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/plugincore.moeci.com;
# proxy
location / {
proxy_pass http://localhost:5007;
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}参考:
如果是使用一层cdn,那么在nginx的配置文件 http{} 字段中加入
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;即可。
如果是使用了两层或以上cdn(使用了cdn和web防火墙也适用),那么在nginx的配置文件http{}字段中加入
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;
real_ip_recursive on;即可。
感谢帮助!
本文作者: yiyun
本文链接: https://moeci.com/posts/分类-Web/nginx/
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!