linux nginx,nginx


安装nginx

** yum系列**

yum install nginx

apt系列

apt install nginx

配置

nginx配置文件位置   /etc/nginx/
通常配置文件都为	/etc/nginx/nginx.conf

有些默认的配置文件是无法启动nginx的 具体如下

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
		........
}

这里的 是ipv6的 但是这行会导致nginx 无法启动 nginx状态查询命令 service redis status

listen       [::]:80 default_server;

解决办法
直接注释即可 以下为示例

server {
        listen       80 default_server;
        # listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
		........
}

添加映射

需要添加以下代码 如有多个请添加多个server 节点
具体server节点的添加位置请根据具体情况而定
不一定是添加在/etc/nginx/nginx.conf 这个文件中 也可能在/etc/nginx/ 中的其他文件中
具体请看/etc/nginx/nginx.conf 中引用其他文件的情况

server {
        listen       80; # 这里是监听的端口号
        server_name  imsjw.com www.imsjw.com; # 这里是监听的域名  多个域名的话以空格隔开

        location / {
            proxy_pass http://127.0.0.1:8080;  # 这里是真实路径
            proxy_connect_timeout 900s;
			proxy_send_timeout 900s;
            proxy_read_timeout 900s;
		}
}

https配置

具体的https 请根据阿里云或者腾讯云官方文档进行配置
官方文档中有详细教程
配置方式不同不做详细讲解,以下介绍端口由80转到443端口

server{
	listen 80;
	server_name imsjw.com www.imsjw.com;
	location / {
		rewrite ^(.*)$ https://$host$1 last;
	}
}

server {
	listen 443;
        server_name imsjw.com www.imsjw.com;
	ssl on;
	ssl_certificate /etc/nginx/ssl/ssl文件.pem;
	ssl_certificate_key /etc/nginx/ssl/ssl文件.key;
	ssl_session_timeout 5m;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_prefer_server_ciphers on;

        location / {
            proxy_pass http://127.0.0.1:8080;
        }
}

http 强行跳转到 https

server {
	listen 80;

	server_name x-s.tech www.x-s.tech;

	location / {
		rewrite ^(.*)$ https://$host$1 last;
	}
}

server {
	listen 443;
        server_name x-s.tech www.x-s.tech;
	ssl on;
	ssl_certificate /etc/nginx/秘钥文件.pem;
	ssl_certificate_key /etc/nginx/秘钥文件.key;
	ssl_session_timeout 5m;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_prefer_server_ciphers on;

        location / {
            proxy_pass http://127.0.0.1:11111;
        }
}

上传文件大小限制配置

server {
        listen       80;
        server_name  imsjw.com www.imsjw.com x-s.tech www.x-s.tech;
		# 这个是设置上传的文件大小限制
		client_max_body_size  500m;
		
        location / {
            proxy_pass http://127.0.0.0:8080;
            proxy_connect_timeout 900s;
	    	proxy_send_timeout 900s;
            proxy_read_timeout 900s;
        }
    }

完整配置文件

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        # listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    server {
        listen       80;
        server_name  imsjw.com www.imsjw.com x-s.tech www.x-s.tech;
		client_max_body_size  500m;
		
        location / {
            proxy_pass http://127.0.0.0:8080;
            proxy_connect_timeout 900s;
	    	proxy_send_timeout 900s;
            proxy_read_timeout 900s;
        }
    }

}


nginx 命令

热加载

nginx -s reload 热更新配置文件 在不影响原先项目运行情况下 更新配置

查看版本

nginx -v

linux nginx 相关命令

启动nginx
service redis start
重启nginx
service redis restart
停止nginx
service redis stop
nginx服务状态查看
service redis status

相关内容

    暂无相关文章