Operating System: Cent OS 6.4 64bit
I installed nginx on my server along with PHP and MySQL, I went through the configuration in the nginx config file, set my server name and listening on port 80.
However, I try to access the server by URL for the first time and I got the error
"The connection to * was interrupted."
Here is my config file from /etc/nginx/conf.d/default.conf (Note: I already set the servername to a subdomain.)
#
# The default server
#
server {
listen 80;
server_name *;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
proxy_pass http://127.0.0.1;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root /usr.share/nginx/html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Any ideas why? Or where can I start finding the issue?
Just guessing.
You can't simply run Apache and Nginx on the same machine - both listening on port 80.
Can two applications listen to the same port?
That message means there are issues establishing websocket connections. Check that you have properly configured nginx to allow websocket connections through.
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
Check your logs with
sudo tail -f /var/log/nginx/access.log /var/log/nginx/error.log
Related
I am working on enabling http2 in nginx docker. I am getting this error when I calling to localhost. My nginx configuration file as below.
server {
listen 2020 http2;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Http2 listen port is 2020. I have exposed it to 2020 when the container is started.
docker run -d --name nginx -p 80:80 -p 2020:2020 -v
/home/pubudu/work/nginx/nginx-tms/config/conf.d:/etc/nginx/conf.d/:ro
nginx:latest
This is the tutorial link:
How To Set Up Nginx with HTTP/2 Support on Ubuntu 16.04
Do i need to use port 443 ? is it mandatory ?
(I didn't use ssl for this)
If i curl this url, response like below.
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: " to save to a file.
I know that the page is transferred as a binary in http2. I want get the nginx index page as a response.
your help will be really appreciated.
Curl does not support HTTP/2 over HTTP (h2c) directly and insists on the upgrade method as detailed in this post:
Over an http:// URL
If CURLOPT_HTTP_VERSION is set to CURL_HTTP_VERSION_2_0, libcurl will include an upgrade header in the initial request to the host to allow upgrading to HTTP/2. Possibly we can later introduce an option that will cause libcurl to fail if not possible to upgrade. Possibly we introduce an option that makes libcurl use HTTP/2 at once over http://
Nginx does not support HTTP/2 and HTTP/1 over the same clear text channel (see this enhancement request) hence it does not work with curl.
You can try using nghttp2 or h2c instead of curl but not sure if that works though not sure if they do. The reality is HTTP/2 is best negotiated over HTTPS and many tools (including all the browsers) only use HTTP/2 over HTTPS because of this.
I have found the solution for this. It is needed to enable SSL in nginx in order to achieve http2. I tried it and it's working well. I found the answer from this link.
How To Set Up Nginx with HTTP/2 Support on Ubuntu 16.04
Now my config file as below.
server {
listen 443 ssl http2;
server_name localhost;
ssl_certificate /etc/nginx/ssl/nginx-selfsigned.crt;
ssl_certificate_key /etc/nginx/ssl/nginx-selfsigned.key;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
I am following a youtube video on setting up Nginx I'm restarting, it gives when I got the part where
I need to edit the server blocks file (sites-enabled/default) I followed exactly what they did in the video, but when saved and tried to restart:
sudo service nginx restart
I got this error:
Job for nginx.service failed because the control process exited with an error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
So I ran
systemctl status nginx.service
which gave output:
Active: failed (Result: exit-code) since Fri 2018-03-02 01:36:33 EST; 10min ago
I'm running all these on an Ubuntu 16.04 Linux VPS
below is the content of the /etc/nginx/sites-enabled/default file:
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name x.x.x.x;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
/* additions i made*/
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
try_files $uri = 404;
fastcgi_split_path_info ^(.*\.php)(/.*)$;
# With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name;
include fastcgi_params;
}
/*end of additions*/
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
EDIT: They used php5.6 in the video but I am using PHP 7.0 if this is helpful
Something's wrong
Set up error logging to debug by editing /etc/nginx/nginx.conf:
find line with error_log, verify log file location and change the level to debug:
error_log logs/error.log debug;
Try restarting again and inspect the log file for more details.
It seems another server is running on port 443/80. so check the ports
netstat -tpln
Most probably it should be apache.
sudo killall apache2
I am not expert in nginx server and fpm, i suddenly got this error many times but i cannot figure what causing this issue :
I am running on server
Intel Xeon D - 32 GB of RAM - 2 x2TB RAID SOFT - Connection speed 1 Gbps
2017/12/06 23:37:58 [error] 20819#20819: *1 connect() to
unix:/run/php/php7.0-fpm.sock failed (11: Resource temporarily unavailable)
while connecting to upstream, client: 62.33.147.332, server: domainname.com, request: "POST /checkstatus HTTP/2.0", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "domainname.com", referrer: "https://domainname.com/adminpanel/live"
This is my nginx configuration file :
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
# the IP(s) on which your node server is running. I chose port 3000.
upstream nfb_notify {
server 127.0.0.1:8080;
keepalive 8;
}
server {
# SSL configuration
#
listen 443 ssl http2 default_server ;
listen [::]:443 ssl http2 default_server ;
ssl_certificate /etc/nginx/ssl/domainname.com/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/domainname.com/domainname.key;
ssl_dhparam /etc/nginx/ssl/domainname.com/dhparam.pem;
client_max_body_size 128M;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
#root /var/www/tdsteam/webu/public;
root /home/tdsteam/webu/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name domainname.com;
gzip on;
gzip_types application/vnd.apple.mpegurl;
gzip_min_length 20;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
# Notification Service
location ~ /(notification|socket.io)/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://nfb_notify;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location ~ /playlists/.m3u8$ {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
#fastcgi_pass 127.0.0.1:8080;
# With php7.0-fpm:
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 180;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {
listen 80;
listen [::]:80;
#
server_name server3.roolink.net;
#
root /var/www/html;
index index.html index.php;
#
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name domainname.com;
return 301 https://$server_name$request_uri;
}
I tried the following solution but did not work.
https://stackoverflow.com/a/10470827/2873860
Any request to my server will return 502 Bad Gateway
This is php-fpm7 log
root#server3:/var/log# tail -f php7.0-fpm.log
[07-Dec-2017 00:22:03] NOTICE: fpm is running, pid 23627
[07-Dec-2017 00:22:03] NOTICE: ready to handle connections
[07-Dec-2017 00:22:03] NOTICE: systemd monitor interval set to 10000ms
[07-Dec-2017 00:41:28] NOTICE: Terminating ...
[07-Dec-2017 00:41:28] NOTICE: exiting, bye-bye!
[07-Dec-2017 00:41:28] NOTICE: configuration file /etc/php/7.0/fpm/php-fpm.conf test is successful
[07-Dec-2017 00:41:28] NOTICE: fpm is running, pid 24735
[07-Dec-2017 00:41:28] NOTICE: ready to handle connections
[07-Dec-2017 00:41:28] NOTICE: systemd monitor interval set to 10000ms
[07-Dec-2017 00:41:28] NOTICE: fpm is running, pid 24735
[07-Dec-2017 00:41:28] NOTICE: ready to handle connections
[07-Dec-2017 00:41:28] NOTICE: systemd monitor interval set to 10000ms
The website starting again without any error when i restarted the fpm:
systemctl restart php7.0-fpm.service
But once i access one of my web page located here
https://mydomainname.com/live.php
How such php with some javascript code that connect to socket cause the whole server to down ? Is it possible ?
The server down with error 502
I have installed nginx on a linux box I have. The site is here. I can connect to it fine using the external IP from my laptop (on the same network as the server) but my friend in the US says he can't load the page. I tried an online vpn service and it returned an error that curl couldn't connect. How can I let others connect to my server too?
Here is my /etc/nginx/available-sites/default file contents:
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name ilan.duckdns.org;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_index index.php;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
I really don't know how to make this work and my google-fu isn't helping. What am I doing wrong here?
You have to configure your router to redirect an external port to the internal linux server ip:port
Ex: 10.20.30.10:8080 (internet ip) ==> 192.168.0.12:80 (linux server on your network)
I moved from the setup of Apache 2 + Varnish to Nginx alone, and I'm kinda stuck with how I should setup/use ESI as well as fastcgi_cache in this setup.
First of all, the idea of ESI was that we setup a reverse proxy layer in front of the server to cache the cache-able parts of a page, then using esi to retrieve the dynamic parts. In my previous setup Varnish was acting as the reverse proxy and Apache only handles the esi requests when necessary.
My question is that now with Nginx acting as the sole server here, how do I make it to work? Do I need to setup another Nginx instance running as a reverse proxy server or something? I couldn't find any document on this.
The second question is regarding fastcgi_cache. I have set it up as described below but the cache does't seem to work for me, no cache file populated and I always get "MISS". I wonder if it's because I need to set max-age/shared-max-age in each controller for each to work?
fastcgi_cache_path /run levels=1:2 keys_zone=www_mysite_com:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/mysite.com/w/w/w/www/web;
index index.php index.html index.htm;
# Make site accessible from http://www.mysite.com
server_name www.mysite.com;
# Specify a character set
charset utf-8;
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
# h5bp nginx configs
# include conf/h5bp.conf;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Deny access to .htaccess
location ~ /\.ht {
deny all;
}
# Don't log robots.txt or favicon.ico files
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { access_log off; log_not_found off; }
# 404 errors handled by our application, for instance Symfony
error_page 404 /app.php;
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|backend/app|backend/app_dev|config)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME web/$fastcgi_script_name;
fastcgi_param HTTPS off;
fastcgi_cache www_mysite_com;
fastcgi_cache_valid 200 60m;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
}
By default, responses from the Symfony 2 application have a cache control header that disables caching:
Cache-Control: no-cache
If you would like nginx to cache pages you will have to change those headers.
You can find general information about caching in the documentation
The simplest solution is to use the SymfonyFrameworkExtraBundle (you already have it if you use the SF2 standard edition) and use annotations on your controllers and/or actions to specify the cache headers. You can find more info about this approach it the docs for the #Cache annotation.